package com.hongfan.common.utils.encrypt;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Md5加密辅助类
 *
 * @author yjr
 * @since 2022-12-19
 */
public class Md5Utils {

    public static void main(String[] args) {
        String password = Md5Utils.md5Encrypt("1671162051_30250_xEvCfz1fcLvymtIzzse2lvqGscXhDcTCm+8JEyWU1AuQB5aWroAEhFAs8TwZRQte4Yyl4DjjyAiUgpumq/XOvIPwGbRJ4tsFMATqbxhoZNoF9U1/lMFD7Y7BwMa+eJOx2Gg/As4mH1bEehApiSW+RXtqzsffr1fCoTOb50eWsB0=");
        System.out.println(password);
    }

    /**
     * MD5加密
     *
     * @param strOrg 要加密的字符串
     * @return 加密后的字符串
     */
    public static String md5Encrypt(String strOrg) {
        StringBuilder buf = new StringBuilder();
        StringBuilder result = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(strOrg.getBytes());
            byte[] b = md.digest();
            int i;
            for (byte value : b) {
                i = value;
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        int size = 16;
        for (int j = 0; j < size; j++) {
            result.append(buf.substring(j * 2, j * 2 + 2));
            result.append("-");
        }
        return result.substring(0, result.length() - 1);
    }

}